﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Computer_Manager.DTO;

namespace Computer_Manager.DAL
{
    public class DAL_categories
    {
        DbProvider db = new DbProvider();
        SqlCommand cmd;
        
        public void categories_insert(DTO_categories obj) 
        {
            db.Open();
            cmd = new SqlCommand("categories_Insert", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@codes_cat", SqlDbType.VarChar).Value = obj.codes_cat;
            cmd.Parameters.Add("@name_cat", SqlDbType.NVarChar).Value = obj.name_cat;
            cmd.Parameters.Add("@parent_id", SqlDbType.Int).Value = obj.parent_id;
            cmd.ExecuteNonQuery();
            cmd.Dispose();

        }

        public void categories_Update(DTO_categories dt)
        {
            db.Open();
            cmd = new SqlCommand("categories_Update", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cat", SqlDbType.Int).Value = dt.id_cat;
            cmd.Parameters.Add("@codes_cat", SqlDbType.VarChar).Value = dt.codes_cat;
            cmd.Parameters.Add("@name_cat", SqlDbType.NVarChar).Value = dt.name_cat;
            cmd.Parameters.Add("@parent_id", SqlDbType.Int).Value = dt.parent_id;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }

        public void categories_Delete(DTO_categories dt)
        {
            db.Open();
            cmd = new SqlCommand("categories_Delete", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cat", SqlDbType.Int).Value = dt.id_cat;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            db.getConn().Close();
        }

        public DataTable GetAll()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("categories_SelectAll", db.getConn());
            DataTable table = new DataTable();
            adapter.Fill(table);
            adapter.Dispose();
            return table;
        }

        public DataTable GetByID(DTO_categories dt)
        {
            cmd = new SqlCommand("categories_GetByID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cat", SqlDbType.Int).Value = dt.id_cat;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetByPatentID(DTO_categories dt)
        {
            cmd = new SqlCommand("categories_GetByParentID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@parent_id", SqlDbType.Int).Value = dt.parent_id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetByParentCat(DTO_categories dt)
        {
            cmd = new SqlCommand("product_GetByParentCat", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@parent_id", SqlDbType.Int).Value = dt.parent_id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
    }
}
